using UnityEngine;
using QFramework;
using QAssetBundle;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
    public partial class Player : ViewController
    {
        [Header("玩家移动速度")]
        [SerializeField]
        float float_PlayerMoveSpeed = 5;

        public static Player Default;

        void Awake()
        {
            Default = this;
        }

        void OnDestroy()
        {
            Default = null;
        }

        void Start()
        {

             //玩家生命
            Global.PlayerHp.RegisterWithInitValue((hp) =>
            {
                hpValue.fillAmount = hp / (float)Global.PlayerMaxHp.Value;

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            // Code Here
            // "Hello QFromWork!".LogInfo();

            //删除player对象
            HurtBox.OnTriggerEnter2DEvent((collider2D =>
            {

                var hitBox = collider2D.GetComponent<HitBox>();
                if (hitBox)
                {
                    if (hitBox.owner.CompareTag("Enemy"))
                    {
                        Global.PlayerHp.Value--;
                        if (Global.PlayerHp.Value <= 0)
                        {
                            var EnemyGre = FindAnyObjectByType<EnemyGenerator>();
                            EnemyGre.OnRefreshWaveCount();
                            Time.timeScale = 0;
                            AudioKit.PlaySound(Sfx.DIE);
                            UIKit.OpenPanel<UIGameOverPanel>();
                            this.DestroyGameObjGracefully();
                        }
                        else
                        {
                            AudioKit.PlaySound(Sfx.HURT);
                        }
                    }
                }
            })).UnRegisterWhenGameObjectDestroyed(gameObject);//当持有改脚本的游戏对象被Destroy的时候注销注册
        }

        private void FixedUpdate()
        {
            float x = Input.GetAxis("Horizontal");
            float y = Input.GetAxis("Vertical");

            if (x < 0)
            {
                Sprite.flipX = false;
            }
            else if (x > 0)
            {
                Sprite.flipX = true;
            }

            var positionVelocity = new Vector2(x, y).normalized * float_PlayerMoveSpeed;

            SelfRigidbody2D.velocity = Vector2.Lerp(SelfRigidbody2D.velocity, positionVelocity,
            1.0f - Mathf.Exp(-Time.deltaTime * 10));

        }
    }
}
